package ${model_package_controller};

/**
 * Created by lewis on ${.now}.
 */

import com.modules.persistence.DynamicSpecifications;
import com.modules.utils.MessageConstant;
import com.modules.web.CRUDController;
import ${name};
import ${model_package_service}.${model_name}Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lewis on ${.now}.
 */
@Controller
@RequestMapping("/${model_name_entity}")
public class ${model_name}Controller extends CRUDController {
    private static Logger logger	= LoggerFactory.getLogger(${model_name}Controller.class);

    @Autowired
    private ${model_name}Service ${model_name_entity}Service;

    @RequestMapping("/list")
    public String list(Model model){

        return "${model_name_entity}/${model_name_entity}List";
    }

    protected Page onPrepareQueryData(HttpServletRequest request) throws Exception {
        Specification spec = DynamicSpecifications.buildSpecification(request);//查询条件
        PageRequest pageRequest = DynamicSpecifications.buildPageRequest(request,null);//相关的分页
        Page<${model_name}> page = ${model_name_entity}Service.findAll(spec,pageRequest);
        return page;//用来查询相关的数据
    }


    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String showAddPage(Model model) {

        return "${model_name_entity}/${model_name_entity}Edit";
    }
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String showViewPage(Model model) {
        model.addAttribute("states","view");
        return "${model_name_entity}/${model_name_entity}Edit";
    }


    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String showEditPage(String ${model_key},  Model model) {

        return "${model_name_entity}/${model_name_entity}Edit";
    }


    /**
     * 获取指定primary key的实体对象（数据库记录）
     */
    @RequestMapping(value = "/edit/{${model_key}}")
    @ResponseBody
    public ${model_name} getEditData(@PathVariable String ${model_key}) {
        ${model_name} ${model_name_entity} = null;
        try {
            ${model_name_entity} = ${model_name_entity}Service.findBy${model_key_by}(${model_key});
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        return ${model_name_entity};
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public Map<String, Object> save(${model_name} ${model_name_entity}) {
        Map<String, Object> tips = new HashMap<String, Object>();
        try {
            String result = ${model_name_entity}Service.save${model_name}(${model_name_entity});
            //判断User是编辑还是保存
            if (MessageConstant.OUT_OF_DATE.equals(result)){
                tips.put(MessageConstant.SUCCESS, false);
                tips.put(MessageConstant.MSG, MessageConstant.FAILURE_OUT_OF_DATE);
            }else {
                tips.put(MessageConstant.SUCCESS, true);
                tips.put(MessageConstant.MSG, MessageConstant.SUCCESS_SAVE);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            tips.put(MessageConstant.SUCCESS, false);
            tips.put(MessageConstant.MSG, MessageConstant.FAILURE_SAVE);
        }
        return tips;
    }
}
